package com.hang.controller;

import cn.hutool.core.util.ObjectUtil;
import com.hang.common.Result;
import com.hang.entity.Role;
import com.hang.entity.User;
import com.hang.exception.MyException;
import com.hang.exception.MyExceptionEnum;
import com.hang.model.dto.request.RoleDTO;
import com.hang.model.dto.request.RoleQueryDTO;
import com.hang.model.dto.response.RoleQueryListVO;
import com.hang.service.IRoleService;
import com.hang.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "角色管理模块")
@RequestMapping("admin/role")
public class RoleController {
    @Resource
    private IRoleService roleService;

    @Resource
    private IUserService userService;

    @ApiOperation(value = "新增角色")
    @PostMapping("create")
    public Result<Object> createRole(
            @RequestBody @Valid RoleDTO roleDTO
    ){
        boolean createResult = roleService.createRole(roleDTO.getRoleName(),roleDTO.getRemarks());
        return createResult==Boolean.TRUE? Result.ok(): Result.fail().message("创建角色失败");
    }

    @ApiOperation(value = "修改角色")
    @PostMapping("update")
    public Result<Object> updateRole(
            @RequestBody @Valid RoleDTO roleDTO
    ){
        boolean updateResult = roleService.updateRole(roleDTO.getRoleId(),roleDTO.getRoleName(),roleDTO.getRemarks());
        return updateResult==Boolean.TRUE? Result.ok(): Result.fail().message("修改角色失败");
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("delete")
    public Result<Object> deleteRole(
            @RequestParam("roleId") Long roleId
    ){
        try {
            deleteOperation(roleId);
        }catch (MyException me){
            return Result.fail().message(me.getMessage());
        }
        return Result.ok().message("删除角色成功");
    }

    @ApiOperation(value = "根据条件搜索角色")
    @PostMapping("getRoleByCondition")
    public Result<RoleQueryListVO> getRoleByCondition(@RequestBody RoleQueryDTO roleQueryDTO){
        RoleQueryListVO roleQueryListVO = roleService.getRoleByCondition(roleQueryDTO);
        return Result.ok(roleQueryListVO);
    }

    @ApiOperation(value = "批量删除角色")
    @DeleteMapping("deleteList")
    @Transactional(rollbackFor = Exception.class)
    public Result<Object> deleteRoleList(@RequestParam("roleList") List<Long> roleList){
        if(roleList.size()<1){
            return Result.fail().message("至少选择一个角色");
        }

        try {
            roleList.forEach(this::deleteOperation);
        }catch (MyException me){
            return Result.fail().message(me.getMessage());
        }
        return Result.ok().message("删除成功");
    }

    private void deleteOperation(Long roleId){
        if(ObjectUtil.isEmpty(roleId)){
            throw new MyException(MyExceptionEnum.ID_IS_NULL);
        }

        //先寻找是否用户仍然属于该部门
        Integer count = userService.lambdaQuery().eq(User::getRoleId, roleId).count();
        if(count > 0){
            throw new MyException(MyExceptionEnum.ROLE_REMOVE_FAIL);
        }
        roleService.lambdaUpdate().eq(Role::getRoleId,roleId).remove();
    }
}
